<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :http-request="upload"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="value" :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <el-progress
        v-if="showProgress"
        type="dashboard"
        :percentage="percent"
        class="progress"
        :color="colors"
      />
    </el-upload>
  </div>
</template>

<style>
.progress {
  position: absolute;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<script>
const COS = require('cos-js-sdk-v5')
const cos = new COS({
  SecretId: 'AKIDrpt5JYm6QoFd5bbdfhu8Nb5a7g1LdvMo', // 身份识别ID
  SecretKey: 'eK1jO5lVMJJQexKmKpLtS4d8dAfBf9bB' // 身份秘钥
})
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      imageUrl: '',
      percent: 0, // 上传进度
      showProgress: false, // 是否展示进度条
      colors: [
        { color: '#f56c6c', percentage: 20 },
        { color: '#e6a23c', percentage: 40 },
        { color: '#5cb87a', percentage: 60 },
        { color: '#1989fa', percentage: 80 },
        { color: '#6f7ad3', percentage: 100 }
      ]
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    upload(res) {
      if (res.file) {
        this.showProgress = true
        // 执行上传操作
        cos.putObject(
          {
            Bucket: 'jinshuzhinanci-1311836198' /* 存储桶 */,
            Region: 'ap-nanjing' /* 存储桶所在地域，必须字段 */,
            Key: res.file.name /* 文件名 */,
            StorageClass: 'STANDARD', // 上传模式, 标准模式
            Body: res.file, // 上传文件对象
            onProgress: (progressData) => {
              console.log(JSON.stringify(progressData))
              this.percent = progressData.percent * 100
            }
          },
          (err, data) => {
            this.showProgress = false
            console.log(err || data)
            // 上传成功之后
            if (data.statusCode === 200) {
              this.$emit('input', `https:${data.Location}`)
              // this.imageUrl = `https:${data.Location}`
            }
          }
        )
      }
    }
  }
}
</script>
